package com.poem.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.poem.constant.MessageConstant;
import com.poem.dto.AuthorsDTO;
import com.poem.dto.AuthorsPageQueryDTO;
import com.poem.entity.Poems;
import com.poem.exception.BaseException;
import com.poem.mapper.AuthorsMapper;
import com.poem.mapper.FavoritesMapper;
import com.poem.mapper.PoemsMapper;
import com.poem.result.PageResult;
import com.poem.service.AuthorsService;
import com.poem.entity.Authors;
import jakarta.annotation.Resource;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @ClassName: AuthorsServiceImpl
 * @Author: YLY
 * @Description:
 * @Date: 2025/10/14
 * @Version: 1.0
 */
@Service
public class AuthorsServiceImpl implements AuthorsService {

    @Resource
    private AuthorsMapper authorsMapper;
    @Resource
    private FavoritesMapper favoritesMapper;
    @Resource
    private PoemsMapper poemsMapper;

    /**
     * 查询所有作者
     * 
     * @return
     */
    public List<Authors> queryAllAuthors() {

        return authorsMapper.queryAllAuthors();

    }

    /**
     * 修改作者
     * 
     * @param authorsDTO
     */
    public void updateAuthor(AuthorsDTO authorsDTO) {
        // 检查作者是否存在
        Authors existingAuthor = authorsMapper.getById(authorsDTO.getId());
        if (existingAuthor == null) {
            throw new BaseException(MessageConstant.AUTHOR_NOT_EXISTS);
        }

        Authors author = new Authors();
        BeanUtils.copyProperties(authorsDTO, author);
        authorsMapper.updateAuthor(author);
    }

    /**
     * 添加作者
     * 
     * @param authorsDTO
     */
    public void addAuthor(AuthorsDTO authorsDTO) {
        Authors author = new Authors();
        BeanUtils.copyProperties(authorsDTO, author);
        authorsMapper.addAuthor(author);
    }

    /**
     * 分页查询
     * 
     * @param authorsPageQueryDTO
     * @return
     */
    public PageResult pageQuery(AuthorsPageQueryDTO authorsPageQueryDTO) {

        PageHelper.startPage(authorsPageQueryDTO.getPage(), authorsPageQueryDTO.getPageSize());

        Page<Authors> page = authorsMapper.pageQuery(authorsPageQueryDTO);

        return new PageResult(page.getTotal(), page.getResult());
    }

    /**
     * 批量删除作者
     * 
     * @param ids
     */
    public void deleteBatch(List<Long> ids) {
        String resourceType = "author";
        for (Long id : ids) {
            // 检查作者是否存在
            Authors author = authorsMapper.getById(id);
            if (author == null) {
                throw new BaseException(MessageConstant.AUTHOR_NOT_EXISTS);
            }

            // 检查是否被收藏
            boolean isFavorited = favoritesMapper.checkFavorite(resourceType, id, null);
            if (isFavorited) {
                throw new BaseException(author.getName() + MessageConstant.AUTHOR_ALREADY_FAVORITES);
            }

            // 检查是否有诗歌关联
            Integer poemCount = poemsMapper.countByAuthorId(id);
            if (poemCount != null && poemCount > 0) {
                throw new BaseException(author.getName() + MessageConstant.AUTHOR_HAS_POEMS);
            }
        }

        authorsMapper.deleteBatch(ids);
    }

    @Override
    public Authors getById(Long id) {

        return authorsMapper.getById(id);
    }
}
